<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <meta http-equiv="content-type" content="text/html;charset=utf-8" />
  <title>Tree</title>

</head>
<body style='line-height:26px;white-space:pre-wrap;'>

sed aaa.log s/date/datedate/

把date替换为datedate

ll | grep -E [0-9]{4}-[0-9]{2} | sed -r s/[0-9]{4}/2012/g
将四位数字替换为2012

ll | grep -E [0-9]{4}-[0-9]{2} | sed -r 1,3s/[0-9]{4}/2012/g
仅将1-3行四位数字替换为2012

sed 2a\bbbbbbbbbbbbbb sed.log
\a 在指定行2后内插入一行

sed 2i\bbbbbbbbbbbbbb sed.log
\i 在指定行2前内插入一行

删除
$ sed '2d'
example-----删除example文件的第二行。

$ sed '2,$d'
example-----删除example文件的第二行到末尾所有行。

$ sed '$d'
example-----删除example文件的最后一行。

$ sed '/test/'d
example-----删除example文件所有包含test的行。

替换：s命令

$ sed 's/test/mytest/g'
example-----在整行范围内把test替换为mytest。如果没有g标记，则只有每行第一个匹配的test被替换成mytest。

$ sed -n 's/^test/mytest/p'
example-----(-n)选项和p标志一起使用表示只打印那些发生替换的行。也就是说，如果某一行开头的test被替换成mytest，就打印它。

$ sed 's/^192.168.0.1/&localhost/'
example-----&符号表示替换换字符串中被找到的部份。所有以192.168.0.1开头的行都会被替换成它自已加localhost，变成192.168.0.1localhost。

$ sed -n 's/\(love\)able/\1rs/p'
example-----love被标记为1，所有loveable会被替换成lovers，而且替换的行会被打印出来。

$ sed 's#10#100#g' example-----不论什么字符，紧跟着s命令的都被认为是新的分隔符，所以，“#”在这里是分隔符，代替了默认的“/”分隔符。表示把所有10替换成100。



sed -r 1,35s/\([0-9]{4}-[0-9]{2}-\)\([0-9]{2}\)/\1/p aaa.log

sed -n s/\([0-9]{4}-[0-9]{2}-\)\([0-9]{2}\)/\1/p aaa.log

for(i in 30)
do
let j=i+1
sed -n s/([0-9]{4}-[0-9]{2}-)$i/\1$j/p aaa.log
done

 sed -re s/\([0-9]{4}-[0-9]{2}-\)\([0-9]{2}\)/\\1`expr '\\2' + 1`/p aaa.log

 for ((i=1;i<32;i++));
 do
 echo $i;
 let j=$i+1
  sed -ri s/\([0-9]{4}-[0-9]{2}-\)\($i\)/\\1$j/ aaa.log
 done

Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]...

  -n, --quiet, --silent
                 suppress automatic printing of pattern space
  -e script, --expression=script
                 add the script to the commands to be executed
  -f script-file, --file=script-file
                 add the contents of script-file to the commands to be executed
  --follow-symlinks
                 follow symlinks when processing in place
  -i[SUFFIX], --in-place[=SUFFIX]
                 edit files in place (makes backup if extension supplied)
  -l N, --line-length=N
                 specify the desired line-wrap length for the `l' command
  --posix
                 disable all GNU extensions.
  -r, --regexp-extended
                 use extended regular expressions in the script.
  -s, --separate
                 consider files as separate rather than as a single continuous
                 long stream.
  -u, --unbuffered
                 load minimal amounts of data from the input files and flush
                 the output buffers more often
      --help     display this help and exit
      --version  output version information and exit

If no -e, --expression, -f, or --file option is given, then the first
non-option argument is taken as the sed script to interpret.  All
remaining arguments are names of input files; if no input files are
specified, then the standard input is read.

GNU sed home page: <http://www.gnu.org/software/sed/>.
General help using GNU software: <http://www.gnu.org/gethelp/>.
E-mail bug reports to: <bug-gnu-utils@gnu.org>.
Be sure to include the word ``sed'' somewhere in the ``Subject:'' field.

</body>
</html>
